最后一遍-L29-divide two integers


Given two integers dividend and divisor,
divide two integers without using multiplication, division, and mod operator.
The integer division should truncate toward zero, which means losing its fractional part. 
For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
Return the quotient after dividing dividend by divisor.
Note: 
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: 
[−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, 
and if the quotient is strictly less than -231, then return -231.
 
Example 1:

Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333.. which is truncated to 3.

Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333.. which is truncated to -2.


Constraints:

 -231 <= dividend, divisor <= 231 - 1
 divisor != 0

按照题目的意思,进行编码

public class L29 {

    public static void main(String[] args) {
        divide(-589, -3);
    }


    public static int divide(int dividend, int divisor) {
        /***
         * 首先jdk中定义int占4个字节 => 32位
         * 32位就是jvm仅仅给分配32个格子的空间,用以存放数据。
         * java中int有正负之分。所以32个格子中占用一个格子标识正负。java 使用补码标识数字
         * 正数的原码,反码,补码全部一样,负数的补码是在原码的基础上,符号位不变,其余各位取反,最后+1
         * 所以仅仅能用31个格子来标识数值。标识位:0标识正 1来标识负。
         * */
//        System.out.println(-1 << 31);
//        System.out.println((1 << 31) - 1);

        /**
         * A constant holding the maximum value an {@code int} can have, 2<sup>31</sup>-1.
         */
//        int maxNum = Integer.MAX_VALUE;
        /**
         * A constant holding the minimum value an {@code int} can have, -2<sup>31</sup>.
         */
//        int minNum = Integer.MIN_VALUE;
//        System.out.println(maxNum);
//        System.out.println(minNum);

        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
        // 修改传入的参数必须为负值
        if(dividend > 0 && divisor > 0){
            return divide(-dividend,-divisor);
        } else if (dividend > 0) {
            return divide(-dividend,divisor);
        } else if (divisor > 0) {
            return divide(dividend,-divisor);
        }else {
            /**
             * 具体的想法是,把589÷-3,变为 (128*3+205)÷3 变为(128*3 + 64*3 + 13)÷3
             * */
            int result=0,currentDivisor = divisor;
            // 因为两个参数都是负数
            while(dividend <= divisor){
                int tmp = 1;
                // 这是针对一个大数,例如-1000000,除以一个小数,例如-1 的情况
                while(dividend <= (currentDivisor <<1) && (currentDivisor << 1) < 0){
                    tmp = tmp << 1;
                    currentDivisor = currentDivisor <<1;
                }

                result = result + tmp;
                dividend = dividend-currentDivisor;
                currentDivisor = divisor;

            }
        }
        return 0;
    }
}

Powered by andiHappy and Theme by AndiHappy